package com.itheima.homework;

import java.util.Set;
import java.util.TreeSet;
/*
*   请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序，
*   如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序，
*   成绩和年龄都一样，则按照姓名的字典顺序排序。
*
* */
public class Test7 {
    public static void main(String[] args) {
        //创建一个treeset集合,储存学生信息
        Set<Student> studentSet = new TreeSet<>();
        Student s1 = new Student("Tom" ,20, 90);
        Student s2 = new Student("Jerry" ,22, 95);
        Student s3 = new Student("John" ,20, 100);
        Student s4 = new Student("Lily" ,22, 100);
        Student s5 = new Student("Lucy" ,22, 90);
        Student s6 = new Student("Kevin" ,22, 90);
        //将学生对象加入集合
        studentSet.add(s1);
        studentSet.add(s2);
        studentSet.add(s3);
        studentSet.add(s4);
        studentSet.add(s5);
        studentSet.add(s6);

        //遍历打印
        studentSet.forEach(s -> System.out.println(s));
    }
}

class Student implements Comparable<Student>{
    private String name;
    private int age;
    private int score;

    public Student() {
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        //对成绩做降序排序
        int result = o.score - this.score;
        //如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序
        result = result == 0 ? this.age - o.age : result;
        //成绩和年龄都一样，则按照姓名的字典顺序排序
        result = result == 0 ? this.name.compareTo(o.name) : result;

        return result;
    }
}
